package com.hexiang.spock

import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.modules.junit4.PowerMockRunnerDelegate
import org.spockframework.runtime.Sputnik
import spock.lang.Specification
import spock.lang.Unroll

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Sputnik.class)
@PrepareForTest([SubService.class])
class AbstractServiceTest extends Specification {
    @Unroll
    def "测试抽象方法"() {
        given: "mock抽象类方法"
        def sub = PowerMockito.mock(SubService)
        PowerMockito.when(sub.parentMethod()).thenReturn(parentValue) // mock掉抽象类的parentMethod, 返回动态mock值:mockParentReturn
        PowerMockito.when(sub.doSomething()).thenCallRealMethod()

        expect: "调用doSomething方法"
        sub.doSomething() == result

        where: "验证分支场景"
        parentValue | result
        "parent1"   | "sub1"
        "parent2"   | "sub2"
        "parent3"   | "sub3"
        "parent4"   | "other"
    }
}